package com.sti.platform.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aisino.core.server.BaseService;
import com.aisino.tools.util.page.Page;
import com.aisino.tools.util.page.Pagination;
import com.sti.platform.dao.ExpertDao;
import com.sti.platform.dao.UserDao;
import com.sti.platform.model.Expert;
import com.sti.platform.model.User;
import com.sti.platform.service.ExpertService;
import com.sti.platform.util.excel.UtilExcel;

@Service
public class ExpertServiceImpl extends BaseService implements ExpertService {
	
	@Autowired
	private ExpertDao expertDao;
	@Autowired
	private UserDao userDao;

	@Override
	public List<Expert> queryList(String params, Integer index, Integer row) {
		StringBuilder builder = new StringBuilder();
		String sql = "SELECT t.*,e.name AS \"degree\",c.name AS \"company\",c2.name AS \"recom_com\","
				+ " r.name AS \"major\",n.name AS \"position\" FROM sys_expert t "
				+ " LEFT JOIN sys_degree e ON t.degree_id = e.id"
				+ " LEFT JOIN sys_company c ON t.company_id = c.id "
				+ " LEFT JOIN sys_company c2 ON t.recom_com_id = c2.id "
				+ " LEFT JOIN sys_major r ON t.major_id = r.id "
				+ " LEFT JOIN sys_position n ON t.position_id = n.id ";
		builder.append(sql);
		if(params != null && !"".equals(params)){
			builder.append(" and s.name like '%"+params+"%'");
		}
		if(index==null || index==0){//当前页数
			index=1;
		}
		if(row==null || row==0){//每页多少条
			row=10;
		}
		Page page = new Pagination();
		page.setPage(index);
		page.setRow(row);
		return expertDao.queryList(builder.toString(), page);
	}

	@Override
	public Map<String, Object> queryAddInfo() {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("degree", expertDao.queryDegrees());
		map.put("position", expertDao.queryPositions());
		map.put("major", expertDao.queryMaJors());
		map.put("company", userDao.queryCompany());
		return map;
	}

	@Override
	public Expert queryExpert(String expertId) {
		String sql = "SELECT t.*,e.name AS \"degree\",c.name AS \"company\",c2.name AS \"recom_com\","
				+ " r.name AS \"major\",n.name AS \"position\""
				+ " FROM sys_expert t "
				+ " LEFT JOIN sys_degree e ON t.degree_id = e.id"
				+ " LEFT JOIN sys_company c ON t.company_id = c.id "
				+ " LEFT JOIN sys_company c2 ON t.recom_com_id = c2.id "
				+ " LEFT JOIN sys_major r ON t.major_id = r.id "
				+ " LEFT JOIN sys_position n ON t.position_id = n.id "
				+ " where t.id = '" + expertId + "'";
		List<Expert> experts = expertDao.queryExperts(sql);
		return experts.get(0);
	}

	@Override
	public void addExpert(Expert expert) {
		super.save(expert);
	}

	@Override
	public void updateExpert(Expert expert) {
		super.update(expert);
	}

	@Override
	public void delectExpert(String expertId) {
		super.remove(Expert.class, expertId);
	}

	@Override
	public void delectExperts(String expertIds) {
		List<String> ids = Arrays.asList(expertIds.split(","));
		for(String id:ids){
			this.delectExpert(id);
		}
	}

	@Override
	public void exportExpert(HttpServletResponse response) {
		UtilExcel<Expert> ue = new UtilExcel(Expert.class);
		String sql = "SELECT t.*,e.name AS \"degree\",c.name AS \"company\",c2.name AS \"recom_com\","
				+ " r.name AS \"major\",n.name AS \"position\""
				+ " FROM sys_expert t "
				+ " LEFT JOIN sys_degree e ON t.degree_id = e.id"
				+ " LEFT JOIN sys_company c ON t.company_id = c.id "
				+ " LEFT JOIN sys_company c2 ON t.recom_com_id = c2.id "
				+ " LEFT JOIN sys_major r ON t.major_id = r.id "
				+ " LEFT JOIN sys_position n ON t.position_id = n.id ";
		List<Expert> list = expertDao.queryExperts(sql);
		if(!list.isEmpty()){
			ue.getListToExcel(list,"专家列表","专家列表",response);
		}
	}

}
